package practise.leetcode.q6;

public class Solution {
    public String convert(String s, int numRows) {
        if (s.isEmpty()) {
            return "";
        }

        if (numRows < 1) {
            return "";
        }

        if (numRows == 1) {
            return s;
        }

        StringBuilder[] builders = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            builders[i] = new StringBuilder();
        }

        char[] chars = s.toCharArray();
        boolean isDown = false;
        int curRow = 0;
        for (char c : chars) {
            builders[curRow].append(c);
            if (curRow == 0 || curRow == numRows -1) {
                isDown = !isDown;
            }
            curRow += (isDown ? 1: -1);
        }

        String result = "";
        for (int i = 0; i < builders.length; i++) {
            result += builders[i].toString();
        }
        return result;
    }
}
